package xiaoqi.springboot.boot.rpc.service;

import org.apache.dubbo.config.annotation.Service;
import xiaoqi.springboot.boot.rpc.api.UserRpcService;
import xiaoqi.springboot.boot.rpc.core.ServiceException;
import xiaoqi.springboot.boot.rpc.core.ServiceExceptionEnum;
import xiaoqi.springboot.boot.rpc.dto.UserAddDTO;
import xiaoqi.springboot.boot.rpc.dto.UserDTO;

import javax.validation.ConstraintViolationException;

@Service(version = "${dubbo.provider.UserRpcService.version}")
public class UserServiceImpl implements UserRpcService {

    @Override
    public UserDTO get(Integer id) throws ConstraintViolationException {
        return new UserDTO().setId(id).setName("没有昵称:" + id).setGender(id % 2 +1);
    }

    @Override
    public Integer add(UserAddDTO addDTO) throws ConstraintViolationException {
        if ("chengxq".equals(addDTO.getName())) {
            throw new ServiceException(ServiceExceptionEnum.USER_EXISTS);
        }
        return Math.toIntExact(System.currentTimeMillis() / 1000);
    }
}
